package cn.edu.hstc.wm.ui.fragment;

import java.util.HashSet;
import java.util.Iterator;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.bean.Good;
import cn.edu.hstc.wm.bean.Store;
import cn.edu.hstc.wm.ui.GoOrder;

public class GoOrderDialog extends DialogFragment implements OnClickListener {

	private static GoOrderDialog sDialog = null;
	private static Bundle sBundle = null;

	private Store mStore;
	private HashSet<Good> mGoodsSet;
	private String shoreNumber;
	private String longNumber;
	private String smsContent;
	View rootView;

	/**
	 * 设置显示拨打的电话号码
	 */
	TextView phoneNumber, smsNumber;

	EditText smsEditText;
	/**
	 * 拨打电话,发送短信
	 */
	ImageButton callPhone, sendSMS;

	public static GoOrderDialog newInstance(Bundle bundle) {
		if (sDialog == null || bundle != sBundle) {
			sDialog = new GoOrderDialog();
			sBundle = bundle;
		}
		return sDialog;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (getDialog() != null) {
			getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // 设置对话框无标题
			getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent); // 设置背景透明
		}

		rootView = inflater.inflate(R.layout.go_order_dialog, container, false);
		initView();
		getData();
		setView();
		return rootView;
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	public void onStart() {
		super.onStart();
		// change dialog width
		if (getDialog() != null) {

			int fullWidth = getDialog().getWindow().getAttributes().width;

			// 版本选择
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
				Display display = getActivity().getWindowManager().getDefaultDisplay();
				Point size = new Point();
				display.getSize(size);
				fullWidth = size.x;
			} else {
				Display display = getActivity().getWindowManager().getDefaultDisplay();
				fullWidth = display.getWidth();
			}

			final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics());

			int w = fullWidth - padding;
			int h = getDialog().getWindow().getAttributes().height;

			getDialog().getWindow().setLayout(w, h); // 设置位置
			getDialog().setCanceledOnTouchOutside(true); // 触摸边界外取消
		}
	}

	private void initView() {

		phoneNumber = (TextView) rootView.findViewById(R.id.tv_go_order_dialog_phone_number);
		smsNumber = (TextView) rootView.findViewById(R.id.tv_go_order_dialog_sms_number);
		smsEditText = (EditText) rootView.findViewById(R.id.et_go_order_dialog_sms_content);
		callPhone = (ImageButton) rootView.findViewById(R.id.ib_go_order_dialog_call);
		sendSMS = (ImageButton) rootView.findViewById(R.id.ib_go_order_dialog_sms_send);

		callPhone.setOnClickListener(this);
		sendSMS.setOnClickListener(this);

	}

	private void setView() {
		phoneNumber.setText(shoreNumber);
		smsNumber.setText(longNumber);
		smsEditText.setText(smsContent);
	}

	@SuppressWarnings("unchecked")
	private void getData() {

		mStore = (Store) sBundle.getSerializable(GoOrder.GO_ORDER_STORE);
		mGoodsSet = (HashSet<Good>) sBundle.getSerializable(GoOrder.GO_ORDER_GOODS_LIST);
		// 不再做正则匹配了,因为数据时从服务器来的,服务器的数据在进入前就应该判断一下
		shoreNumber = mStore.getShort_number() != null ? mStore.getShort_number() : mStore.getLong_number();
		longNumber = mStore.getLong_number() != null ? mStore.getLong_number() : mStore.getShort_number();

		Iterator<Good> iterator = mGoodsSet.iterator();
		Good good;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < mGoodsSet.size(); i++) {
			if (iterator.hasNext()) {
				good = iterator.next();
				sb.append(good.getSelectCount() + "份" + good.getName() + ";");
			}
		}

		// 自动生成短信内容
		// TODO 加上用户地址,时间等信息
		smsContent = "老板,我想要:" + sb;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_go_order_dialog_call:
			callPhone();
			break;
		case R.id.ib_go_order_dialog_sms_send:
			sendSms();
			break;

		default:
			break;
		}
	}

	/**
	 * 拨打电话
	 */
	private void callPhone() {
		Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + shoreNumber));
		startActivity(callIntent);

		// TODO 记录
	}

	/**
	 * 生成短信发送
	 */
	private void sendSms() {
		Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + shoreNumber));
		sendIntent.putExtra("sms_body", smsContent);
		startActivity(sendIntent);
		// TODO 记录
	}

}
